package com.tqjc.entity;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.tqjc.exception.TaskRunningException;
import com.tqjc.system.core.annotation.QueryField;
import com.tqjc.system.core.model.QueryCondition;
import com.tqjc.work.TaskCallback;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.Date;


@TableName(value = "task_apart")
@Data
@EqualsAndHashCode(callSuper=false)
@DS("xianggang")
@Schema(description = "任务部分表")
@Slf4j
public class TaskApart extends Model implements InitializingBean, DisposableBean {



    /**
     * 主键
     */
    @TableId(type = IdType.AUTO)
    @Schema(description = "任务明细主键ID")
    private int id;
    /**
     * 所属任务id
     */
    @Schema(description = "所属上级任务id")
    @QueryField(queryColumn="task_id", condition= QueryCondition.eq)
    private Integer taskId;

    /**
     * 天车类型
     */
    @Schema(description = "天车编号")
    private String craneType;
    /**
     * 任务类型
     */
    @Schema(description = "任务类型")
    private String type;
    /**
     * 开始位置x
     */
    @Schema(description = "起始大车位置")
    private Integer beginX;
    /**
     * 开始位置y
     */
    @Schema(description = "起始位置小车位置")
    private Integer beginY;
    /**
     * 开始位置z
     */
    @Schema(description = "起始位置高度")
    private Integer beginZ;
    /**
     * 结束位置x
     */
    @Schema(description = "结束大车位置")
    private Integer endX;
    /**
     * 结束位置y
     */
    @Schema(description = "结束小车位置")
    private Integer endY;
    /**
     * 结束位置z
     */
    @Schema(description = "结束位置高度")
    private Integer endZ;

    /**
     * 钢板型号
     */
    @Schema(description = "钢板型号")
    private String steelType;

    /**
     * 钢板id
     */
    @Schema(description = "钢板编号")
    private String steelId;

    /**
     * 磁砣模式
     */
    @Schema(description = "磁砣模式")
    private Integer workMode;
    /**
     * 开始时间
     */
    @Schema(description = "开始时间")
    private Date beginTime;
    /**
     * 结束时间
     */
    @Schema(description = "结束时间")
    private Date endTime;

    /**
     * 上层Task
     */
    @TableField(exist = false)
    @Schema(description = "上层Task")
    private Task task;

    @TableField(exist = false)
    private TaskCallback callback;

    public void execute() {
        System.out.println("执行任务...");
        if (callback != null) {
            callback.onPreExecute();
        }
        action();
        if (callback != null) {
            callback.onPostExecute();
        }
    }

    public void setCallback(TaskCallback callback) {
        this.callback = callback;
    }

    /**
     * create by: pzy
     * description: 执行任务
     * create time: 2023/5/22 10:06
     *
     * @return java.util.concurrent.Future<com.tqjc.execute.utils.PublicResult>
     * @param: []
     */
    public void action() {
        //执行前置任务
        getCallback().onPreExecute();

        try {
            log.info("{}步骤开始执行",getType());
            customAction();
            //清楚Bean对象
            log.info("{}步骤执行完成",getType());
            Thread.sleep(1000);


        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (TaskRunningException e) {
            log.error("运行时异常",e);
            e.printStackTrace();
        }

        //执行后置任务
        getCallback().onPostExecute();

    }


    public void customAction() throws TaskRunningException {
        //执行前置任务
        getCallback().onPreExecute();

        try {
            Thread.sleep(1000);
            System.out.println("正在执行任务" + getType());

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //执行后置任务
        getCallback().onPostExecute();

    }


    /**
     * create by: pzy
     * description: 根据任务生成步骤
     * create time: 2023/5/26 11:19
     *
     * @return void
     * @param: []
     */
    public void setTaskApartByTask(Task task) {

        this.setTaskId(task.getId());
        this.setBeginX(task.getBeginX());
        this.setBeginY(task.getBeginY());
        this.setBeginZ(task.getBeginZ());
        this.setEndX(task.getEndX());
        this.setEndY(task.getEndY());
        this.setEndZ(task.getEndZ());
        this.setTask(task);

    }

    @Override
    public void destroy() throws Exception {

    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }

//    public List<Roller> getRollerByXAndY


}